package strategy.browser;


import strategy.Node;

import java.util.List;
import java.util.Stack;

public class DFSTreeBrowser implements TreeBrowser {

    @Override
    public void browse(Node root, Node.INodeAction nodeAction) {
        final Stack<Node> stack = new Stack<Node>();
        stack.push(root);

        while (!stack.isEmpty()) {
            Node node = stack.pop();
            nodeAction.doAction(node);
            List<Node> children = node.getChildren();
            for (int i = children.size() - 1; i >= 0; i--) {
                stack.push(children.get(i));
            }
        }

    }
}
